<?php

/**
 * 运费规则设置
 * @author renjianhong
 * @date 2021-08-08 10:47
 */
namespace Modules\Mallsetting\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Mallsetting\Http\Controllers\Controller;
use Modules\Mallsetting\Entities\PostageRules;
use Modules\Mallsetting\Entities\Area;


class PostageRulesController extends Controller
{
    /**
     * 运费规则列表
     */
    public function list(Request $request , PostageRules $PostageRulesModel)
    { 
        return view('mallsetting::admin.postage_rules.list',compact('PostageRulesModel'));
    }

    /**
     * AJAX获取列表数据
     */
    public function ajaxList(Request $request ,PostageRules $PostageRulesModel)
    {
        $pagesize = $request->input('limit'); // 每页条数
        $page = $request->input('page',1);//当前页
        $where = [
            ['is_delete', '=', '0'],
        ];
        //获取总条数
        $count = $PostageRulesModel
                ->where($where)
                ->count();
        
        //求偏移量
        $offset = ($page-1)*$pagesize;
        $list = $PostageRulesModel
                ->where($where)
                ->orderByDesc('id')->offset($offset)->limit($pagesize)->get();
        
        return $this->success(compact('list', 'count'));
    }

    /**
     * 新增编辑运费规则
     */
    public function edit(Request $request, PostageRules $PostageRulesModel)
    {
        if($request->isMethod('post')){
            $data = $request->input();
            
            if(isset($data['detail'])){
                $detail = array_values($data['detail']);
                $data['detail'] = json_encode($detail);
            }else{
                return $this->failed("请增加规则条目");
            }
            
            if(!$data['id']){
                $data['created_at'] = date('Y-m-d H:i:s');
                $reulst = $PostageRulesModel->insertGetId($data);
            }else{
                $reulst = $PostageRulesModel->where('id',$data['id'])->update($data);
            }
            if($reulst){
                return $this->success("操作成功");
            }else{
                return $this->failed("操作失败");
            }
        }
        $id = $request->input('id');
        $info = $PostageRulesModel->where('id',$id)->first();
        $key_val = 0;
        if($info){
            $info->detail = array_values(json_decode($info->detail,true));//索引从0开始排序
            $list = $info->detail;
            $numItems = count($list);
            $i = 0;
            foreach($list as $key => $v){
                if(++$i === $numItems) {
                    $key_val = $numItems;
                }
            }
        }
        
        return view('mallsetting::admin.postage_rules.edit',compact('info','key_val','PostageRulesModel'));
    }

    /**
     * 获取省份区域
     */
    public function getArea(Area $AreaModel)
    {
        //获取所有省份
        $province_list = $AreaModel->selectRaw("id,parent_id,name as title")->whereIn('level',['province','city'])->get()->toArray();
        $list = $this->buildMenuChild(1, $province_list);
        
        return $this->success(compact('list'));
    }

    /**
     * 递归获取子菜单
     */
    private function buildMenuChild($id, $province_list,$lst=0){
        
        $treeList = [];
        foreach ($province_list as $key => $v) {
            if ($id == $v['parent_id']) {
                $node = (array)$v;
                unset($province_list[$key]);

                $child = $this->buildMenuChild($v['id'], $province_list,2);
                if($child){
                    $node['children'] = $child;
                    $node['spread'] = false;//初始化节点是否展开
                }
                // todo 后续此处加上用户的权限判断
                $treeList[] = $node;
                
            }
        }
        
        return $treeList;
    }

    /**
     * 删除运费规则
     */
    public function delete(Request $request, PostageRules $PostageRulesModel){
        if($request->isMethod('post')){
            $id = $request->input('id');
            if($id){
                $reulst = $PostageRulesModel->where('id',$id)->update(['is_delete'=>1]);
                if($reulst){
                    return $this->success('操作成功');
                }else{
                    return $this->failed('操作失败');
                }
            }else{
                return $this->failed('缺少参数id');
            }
        }
    }

    /**
     * 批量删除
     */
    public function batchDelete(Request $request, PostageRules $PostageRulesModel){
        if($request->isMethod('post')){
            $id = $request->input('id');
            if($id){
                $reulst = $PostageRulesModel->whereIn('id',$id)->update(['is_delete'=>1]);
                if($reulst){
                    return $this->success('操作成功');
                }else{
                    return $this->failed('操作失败');
                }
            }else{
                return $this->failed('请勾选要删除的数据');
            }
            
        }
    }

    /**
     * 设置默认规则
     */
    public function isDefault(Request $request, PostageRules $PostageRulesModel)
    {
        if($request->isMethod('post')){
            $is_default = $request->input('is_default');
            $id = $request->input('id');
            if($is_default == 'true'){
                $list = $PostageRulesModel->where('is_default',1)->get();
                foreach($list as $key => $v){
                    $v->is_default = 0;
                    $v->save();
                }
                //启用
                $reulst = $PostageRulesModel->where('id',$id)->update(['is_default'=>1]);
            }else{
                //禁用
                $reulst = $PostageRulesModel->where('id',$id)->update(['is_default'=>0]);
            }
            
            if($reulst){
                return $this->success('操作成功');
            }else{
                return $this->failed('操作失败');
            }
        }
        
    }
}